package com.ruoyi.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.mes.vo.wksp.RptVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.domain.TaskCart;
import com.ruoyi.mes.service.ITaskCartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车间任务卡Controller
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Api(tags = "车间任务卡")
@RestController
@RequestMapping("/mes/taskcart")
public class TaskCartController extends BaseController {
    @Autowired
    private ITaskCartService taskCartService;

    /**
     * 查询车间任务卡列表
     */
    @ApiOperation(value = "查询车间任务卡列表")
    @PreAuthorize("@ss.hasPermi('mes:taskcart:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaskCart taskCart) {
        startPage();
        List<TaskCart> list = taskCartService.selectTaskCartList(taskCart);
        return getDataTable(list);
    }

    /**
     * 导出车间任务卡列表
     */
    @ApiOperation(value = "导出车间任务卡列表")
    @PreAuthorize("@ss.hasPermi('mes:taskcart:export')")
    @Log(title = "车间任务卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskCart taskCart) {
        List<TaskCart> list = taskCartService.selectTaskCartList(taskCart);
        ExcelUtil<TaskCart> util = new ExcelUtil<TaskCart>(TaskCart.class);
        util.exportExcel(response, list, "车间任务卡数据");
    }

    /**
     * 获取车间任务卡详细信息
     */
    @ApiOperation(value = "获取车间任务卡详细信息")
    @PreAuthorize("@ss.hasPermi('mes:taskcart:query')")
    @GetMapping(value = "/{taskCardId}")
    public AjaxResult getInfo(@PathVariable("taskCardId") Long taskCardId) {
        return success(taskCartService.selectTaskCartByTaskCardId(taskCardId));
    }

    /**
     * 新增车间任务卡
     */
    @ApiOperation(value = "新增车间任务卡")
    @PreAuthorize("@ss.hasPermi('mes:taskcart:add')")
    @Log(title = "车间任务卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskCart taskCart) {
        return toAjax(taskCartService.insertTaskCart(taskCart));
    }

    /**
     * 修改车间任务卡
     */
    @ApiOperation(value = "修改车间任务卡")
    @PreAuthorize("@ss.hasPermi('mes:taskcart:edit')")
    @Log(title = "车间任务卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskCart taskCart) {
        return toAjax(taskCartService.updateTaskCart(taskCart));
    }

    /**
     * 删除车间任务卡
     */
    @ApiOperation(value = "删除车间任务卡")
    @PreAuthorize("@ss.hasPermi('mes:taskcart:remove')")
    @Log(title = "车间任务卡", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskCardIds}")
    public AjaxResult remove(@PathVariable Long[] taskCardIds) {
        return toAjax(taskCartService.deleteTaskCartByTaskCardIds(taskCardIds));
    }

    //*********************

    /**
     * 查 - 任务卡详情
     * @param id 任务卡id
     * @return
     */
    @GetMapping("/getDetail")
    public AjaxResult getDetail(@RequestParam Long id){
        return taskCartService.getDetail(id);
    }

    /**
     * 查 - 已排序的工序
     * @param id 任务卡id
     * @return
     */
    @GetMapping("/sorted")
    public AjaxResult sorted(@RequestParam Long id){
        return taskCartService.sorted(id);
    }

    /**
     * 报工
     * @return
     */
    @PostMapping("/report")
    public AjaxResult report(@RequestBody RptVo rptVo){
        return taskCartService.report(rptVo);
    }

    /**
     * 查 - 任务卡分页 - 任务卡编码不重复
     * @param taskCart
     * @return
     */
    @GetMapping("/taskCardList")
    public TableDataInfo taskCardList(TaskCart taskCart){
        startPage();
        List<TaskCart> list = taskCartService.taskCardList(taskCart);
        return getDataTable(list);
    }

    /**
     * 查 - 可报工数量
     * @param id 任务卡id
     * @param proceSeque 报工顺序
     * @return
     */
    @GetMapping("/getTransferredQuantity")
    public AjaxResult getTransferredQuantity(@RequestParam Long id,@RequestParam Long proceSeque){
        return taskCartService.getTransferredQuantity(id,proceSeque);
    }
}
